<?php
function post_request($url, $data, $referer='') {
 
	$port = 80;
    // Convert the data array into URL Parameters like a=b&foo=bar etc.
    //$data = http_build_query($data);
 	//$data = "ou=rue%20d%20anglemont%2093260%20LES%20LILAS&btnValidSearch=true&j%5Fidt673=searchFormPBexpress&prenom=formExpress";
    // parse the given URL
    $url = parse_url($url);

    if ($url['scheme'] == 'https') { 
       $port = 443;
       $host = preg_replace("/https/","ssl",$url['host']);
    }
	else 
	{
    	// extract host and path:
  	  	$host = $url['host'];
	}
    $path = $url['path'];
 	var_dump($data,$port);
 	
    // open a socket connection on port 80 - timeout: 30 sec
    $fp = fsockopen($host, $port, $errno, $errstr, 30);
 
    if ($fp){
 
    	// Request
    	$request  = "POST $path HTTP/1.1\r\n";
    	$request .= "Host: $host\r\n";
    	$request .= "Content-type: application/x-www-form-urlencoded\r\n";
    	$request .= "Content-length: ". strlen($data) ."\r\n";
    	$request .= "Connection: close\r\n\r\n";
    	
    	/*
    	// Request V2
    	$http  = "POST /index.php HTTP/1.1\r\n";
	    $http .= "Host: " . $_SERVER['HTTP_HOST'] . "\r\n";
	    $http .= "User-Agent: " . $_SERVER['HTTP_USER_AGENT'] . "\r\n";
	    $http .= "Content-Type: application/x-www-form-urlencoded\r\n";
	    $http .= "Content-length: " . strlen($data) . "\r\n";
	    $http .= "Connection: close\r\n\r\n";
	    $http .= $data . "\r\n\r\n";
    	fwrite($fp, $http);
	    while (!feof($fp)) {
	        $_return .= fgets($fp, 4096);
	    }
	    fclose($fp);
	    echo $_return;
	   */
        // send the request headers:
        fputs($fp, "POST $path HTTP/1.1\r\n");
        fputs($fp, "Host: $host\r\n");
 
        if ($referer != '')
            fputs($fp, "Referer: $referer\r\n");
 
        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
        fputs($fp, "Content-length: ". strlen($data) ."\r\n");
        fputs($fp, "Connection: close\r\n\r\n");
        fputs($fp, $data);
 
        $result = ''; 
        while(!feof($fp)) {
            // receive the results of the request
            $result .= fgets($fp, 128);
        }
        // Post a second request 
        
    }
    else { 
        return array(
            'status' => 'err', 
            'error' => "$errstr ($errno)"
        );
    }
 
    // close the socket connection:
    fclose($fp);
 
    // split the result header from the content
    $result = explode("\r\n\r\n", $result, 2);
 
    $header = isset($result[0]) ? $result[0] : '';
    $content = isset($result[1]) ? $result[1] : '';
 
    // return as structured array:
    return array(
        'status' => 'ok',
        'header' => $header,
        'content' => $content
    );
}